package com.CityHunter.Service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

import com.CityHunter.R;
import com.CityHunter.Request.UpdateLocationRequest;

public class UpdateLocationService extends Service{

	private LocationManager myLocationManager;
	private SharedPreferences sp;
	private int userid;
	private String strLocationProvider = "";
	
	@Override 
	public void onCreate(){
		
		Log.d("Debug","Service created.");
		// Get userid from Share Preference
		sp = getSharedPreferences("cityhunter", 0);
		userid = sp.getInt("userid", 0);

		// Get Location Service
		myLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		
		//Get Location
		Location currentLocation = getLocationProvider(myLocationManager);
		if (currentLocation != null){
			uploadCurrentLocation(currentLocation);
			Log.d("Debug","Current Location = " + currentLocation.toString());
		}
		myLocationManager.requestLocationUpdates(strLocationProvider,2000,10,mLocationListener);
		
		super.onCreate();
	}
	
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public final LocationListener mLocationListener = new LocationListener(){
		@Override
		public void onLocationChanged(Location location){
			Log.d("Debug","Current Location : " + location.toString());
			uploadCurrentLocation(location);
		}

		@Override
		public void onProviderDisabled(String provider) {
			Log.d("Debug","Provider disabled.");
		}

		@Override
		public void onProviderEnabled(String provider) {
			Log.d("Debug","Provider enabled.");
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		
	};
	
	public Location getLocationProvider(LocationManager lm){
		Location retLocation = null;
		try{
			Criteria mCriteria = new Criteria();
			mCriteria.setAccuracy(Criteria.ACCURACY_FINE);
			mCriteria.setAltitudeRequired(false);
			mCriteria.setBearingRequired(false);
			mCriteria.setCostAllowed(true);
			mCriteria.setPowerRequirement(Criteria.POWER_LOW);
			//strLocationProvider = lm.getBestProvider(mCriteria,false);
			strLocationProvider = "network";
			retLocation = lm.getLastKnownLocation(strLocationProvider);
			Log.d("Debug","Best Provider = "+ strLocationProvider);
			
			if (retLocation == null)
				Log.d("Debug","retLocation = null");
			else 
				Log.d("Debug","retLocation = "+ retLocation.toString());
		} catch (Exception e){
			e.printStackTrace();
		}
		return retLocation;
	}
	
	public void uploadCurrentLocation(Location currentLocation){
		// Send Current Location to DB
		String updateLocationPage = getString(R.string.url_updateLocationPage);
		UpdateLocationRequest ulr = new UpdateLocationRequest(updateLocationPage,userid);
		ulr.updateLocation(currentLocation);
	}
	
}
